package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.SysParamBean;
import cc.rengu.igas.bomp.facade.request.ParamQueryRequest;
import cc.rengu.igas.bomp.facade.response.ParamQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxuran on 2020/03/30
 */
public class ParamQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ParamQueryRequest paramQueryRequest = new ParamQueryRequest();
        ConvertUtil.convertOutput(paramQueryRequest);
        return paramQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ParamQueryRequest paramQueryRequest = (ParamQueryRequest) request;
        ParamQueryResponse paramQueryResponse = new ParamQueryResponse();
        /*获取法人行号信息*/
        String instId = paramQueryRequest.getHeader().getInstId();
        /*获取参数类型信息*/
        String paramType = paramQueryRequest.getParamType();
        /*获取参数键值信息*/
        String paramKey = paramQueryRequest.getParamKey();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(paramType)) {
            rglog.error("必填信息为空");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*查询参数信息*/
        SysParamService sysParamService = new SysParamServiceImpl();
        List<SysParamBean> bompSysParamList = new ArrayList<>();
        if (StringUtil.isEmptyOrNull(paramKey)) {
            /*根据参数类型查询参数信息*/
            List<SysParam> sysParamList = sysParamService.getSysParamInfo(instId, paramType);
            if (!CollectionUtils.isEmpty(sysParamList)) {
                sysParamList.forEach(item -> convSysparamInfo(item, bompSysParamList));
            }
        } else {
            /*根据参数类型和参数键值查询参数信息*/
            SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
            if (null != sysParam) {
                convSysparamInfo(sysParam, bompSysParamList);
            }
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<ParamQueryResponse> bizResponse = new BizResponse<>();
        paramQueryResponse.setSysParamList(bompSysParamList);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(paramQueryResponse);
        return bizResponse;
    }

    /**
     * 参数列表信息转换
     *
     * @param sysParam         功能列表参数配置
     * @param bompSysParamList 参数属性信息
     */
    private void convSysparamInfo(SysParam sysParam, List<SysParamBean> bompSysParamList) {
        SysParamBean sysParamBean = new SysParamBean();
        BeanUtil.beanCopy(sysParam, sysParamBean);
        bompSysParamList.add(sysParamBean);
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ParamQueryResponse paramQueryResponse = (ParamQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(paramQueryResponse);
    }
}
